# coding=utf-8
sorter1=sorted([7,-1,0,9,3,2,19]) #sorted按升序排列
print(sorter1)

sorter1 = sorted([1,3,6,-20,34])
print("升序排列:",sorter1)

# sorted()函数也是高阶函数，它还可以接收一个key函数来实现自定义的排序
sorter2 = sorted([1,3,6,-20,-70],key=abs)
print("自定义排序:",sorter2)

sorter2 = sorted([1,3,6,-20,-70],key=abs,reverse=True)
print("自定义反向排序:",sorter2)
# 4.2 字符串排序依照ASCII
sorter3 = sorted(["ABC","abc","D","d"])
print("字符串排序:",sorter3)
# 4.3 忽略大小写排序
sorter4 = sorted(["ABC","abc","D","d"],key=str.lower)
print("忽略字符串大小写排序:",sorter4)
# 4.4 要进行反向排序，不必改动key函数，可以传入第三个参数reverse=True：
sorter5 = sorted(["ABC","abc","D","d"],key=str.lower,reverse=True)
print("忽略字符串大小写反向排序:",sorter5)


from functools import cmp_to_key

class Student:
  def __init__(self, age, name):
    self.name = name
    self.age = age

def custom_sorted(stu1,stu2):
  if stu1.age < stu2.age:
    return -1
  if stu1.age > stu2.age:
    return 1
  return 0

stu1 = Student(41, 'aaa')
stu2 = Student(21, 'ccc')
stu3 = Student(31, 'bbb')
# student_list = sorted([stu1, stu2, stu3], key=lambda x: x.age)
student_list = sorted([stu1, stu2, stu3], key=cmp_to_key(custom_sorted))
for stu in student_list:
  print('name:', stu.name, 'age:', stu.age)







